package com.itheima.reflect.myreflect4;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

public class ReflectDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //method1();
        method2();
    }

    private static void method2() throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        Class<?> clazz = Class.forName("com.itheima.reflect.myreflect4.Student");
        Field money = clazz.getDeclaredField("money");
        money.setAccessible(true);
        Student student = (Student) clazz.getConstructor().newInstance();
        Object o = money.get(student);
        System.out.println(o);
    }

    private static void method1() throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        Class<?> clazz = Class.forName("com.itheima.reflect.myreflect4.Student");
        Field name = clazz.getField("name");
        // 1. 先创建一个 Student 对象
        Student student = (Student) clazz.getConstructor().newInstance();
        name.set(student, "zhangsan");

        System.out.println(student);
    }
}
